// pages/mypage/mypage.js
var app = getApp();
var my=require("../../api/my");
var util=require("../../utils/util")


Page({
  /*页面的初始数据*/
  data: {
    navColor: "rgba(0,0,0,0)",
    title: "个人中心",
    showMy: true,
    top_height: 0,
    fun_list:[{url:"/images/icon38.png",title:"个人资料"},{url:"/images/icon39.png",title:"我的宝宝"},{url:"/images/icon40.png",title:"我的账户"},{url:"/images/icon145.png",title:"我的推广码"},{url:"/images/icon146.png",title:"我的团队"},{url:"/images/icon147.png",title:"我的钱包"},{url:"/images/icon41.png",title:"客服中心"},{url:"/images/icon42.png",title:"用户反馈"},{url:"/images/icon43.png",title:"关于家巴啦"},{url:"/images/icon44.png",title:"设置"}],
    my_page: ["../../mypage/myOrder/myOrder","../../mypage/myCall/myCall","../../mypage/myPrefer/myPrefer","../../mypage/myNews/myNews"],
    fun_page: ["../../mypage/personalDom/personalDom","../../mypage/myBaby/myBaby","../../mypage/myAccount/myAccount","../../mypage/reference/reference","../../mypage/myTeam/myTeam","../../mypage/wallet/wallet","../../mypage/customerSer/customerSer","../../mypage/userBack/userBack","../../mypage/about/about","../../mypage/setting/setting"],
    login: false,
    userinfo: null,
    news_count: 0
  },
  /* 生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    var sysInfo = wx.getSystemInfoSync();
    // that.setData({top_height: sysInfo.statusBarHeight})
    if(sysInfo.statusBarHeight >= 44){
      that.setData({top_height: sysInfo.statusBarHeight})
    }
    // var userinfo=wx.getStorageSync('userinfo');
    // if(userinfo){
    //   that.get_personal_dom(userinfo.id)
    // }
  },
  // 去登录
  nav_login:function(){
    wx.navigateTo({
      url: '../login/login',
    })
  },
  // 头像跳转
  nav_jump:function(){
    var that=this,userinfo=that.data.userinfo;
    wx.navigateTo({
      url: '../../mypage/personalDom/personalDom?id='+userinfo.id,
    })
  },
  nav_into:function(){
    wx.navigateTo({
      url: '/mypage/vipMember/vipMember',
    })
  },
  // 顶部模块
  nav_to:function(e){
    var that = this,my_page=that.data.my_page;
    var index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: my_page[index],
    })
  },
  // 下部模块
  nav_fun_to:function(e){
    var that = this,fun_page=that.data.fun_page,userinfo=that.data.userinfo;
    var index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: fun_page[index]+"?id="+userinfo.id,
    })
  },
  // 添加机构
  nav_addmech:function(){
    wx.navigateTo({
      url: '../../mypage/addMech/addMech',
    })
  },
  onShow:function(){
    var that=this;
    var info=wx.getStorageSync('unionId'),login_info=wx.getStorageSync('login_info'),token=wx.getStorageSync('token'),userinfo=wx.getStorageSync('userinfo');
    if(userinfo){
      that.get_personal_dom(userinfo.id)
    }
    if((info&&token)||(login_info&&token)){
      if(info&&token){
        app.globalData.unionId=info.unionId
      }
      var change_phone=app.globalData.change_phone
      var change_dom=app.globalData.change_dom
      that.setData({login:true})
      if(change_phone||change_dom){
        app.globalData.change_phone=false
        app.globalData.change_dom=false
        //
        that.get_personal_dom(userinfo.id)
      }
      that.get_unRead_news(token)
    }else{
      that.setData({userinfo:null,login:false})
    }
  },
  // 获取未读消息
  get_unRead_news:function(token){
    var that=this;
    my.get_unRead_news(token).then(res=>{
      if(res.data.code==200){
        that.setData({news_count: res.data.data.allCount})
      }
    })
  },
  // 个人资料
  get_personal_dom:function(id){
    wx.showLoading({title:"加载中"})
    var that=this;
    my.get_personal_dom({"id":id}).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        info.vip=1
        info.cardNum=util.insertStr(util.insertStr(util.insertStr(util.insertStr(info.id,4," "),9," "),14," "),19," ");
        that.setData({userinfo:info});
      }
    })
  },
})